package com.anli1.controller;


import com.anli1.entity.dto.SessionwebUserDto;
import com.anli1.entity.pojo.User;
import com.anli1.entity.vo.R;
import com.anli1.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

@RestController
@RequestMapping("/api")
public class ExampleController {
    @Autowired
    IUserService iUserService;

    @GetMapping("/login")
    @ApiOperation(value = "登录")

    public R hello(@ApiParam(value = "用户ID", required = true) String account, @ApiParam(value = "用户密码", required = true) String password, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        R r = iUserService.login(account, password);
  /*      if (Objects.nonNull(r.getData())) {
            //登录成功了的
            HttpSession session = req.getSession();
            session.setAttribute("session_account", "1");
        }*/
        if (Objects.nonNull(r.getData())) {
            Cookie cookie = new Cookie("username", (String) r.getData());
            resp.addCookie(cookie);
        }

        return r;
    }



    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public R register(@RequestBody User user) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        return iUserService.register(user);
    }
}
